package common;

import common.util.Anno;
import common.util.KvField;
import common.util.Stu;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : wenchao.long
 * @date : Created in 2024-03-06 下午 1:26 周三
 * @description :
 */
public class T {

	public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//		String string = new StringBuilder("ja").append("va").toString();
//		System.out.println(string);
//		System.out.println(string.intern());
//		System.out.println(string==(string.intern()));
//		getPowerOfTwo(10);

		Stu stu1 = new Stu("code1", "name1");
		Stu stu2 = new Stu("code2", "name2");
		List<Stu> list = new ArrayList<>();
		list.add(stu1);
		list.add(stu2);
		Field[] fields = Stu.class.getDeclaredFields();

		List<List<String>> result = new ArrayList<>();
		for (Stu stu : list) {
			Class<? extends Stu> clazz = stu.getClass();
			List<KvField<Integer, String>> kvFields = new ArrayList<>();
			for (Field field : fields) {
				if (!Modifier.isFinal(field.getModifiers())) {
					Anno annotation = field.getAnnotation(Anno.class);
					String name = field.getName();
					String value = annotation.value();
					int position = annotation.position();

					Field declaredField = clazz.getDeclaredField(name);
					declaredField.setAccessible(true);
					field.setAccessible(true);
					String o = (String)field.get(stu);

					KvField<Integer, String> kvField = new KvField<>();
					kvField.setKey(position);
					kvField.setValue(o);
					kvFields.add(kvField);
				}
			}
			List<String> collect = kvFields.stream()
					.sorted(Comparator.comparing(KvField::getKey))
					.map(KvField::getValue)
					.collect(Collectors.toList());
			result.add(collect);
		}
		System.out.println(result);
	}

	static void getPowerOfTwo(int original) {
		int i = 0;
		int powerOfTwo = 1;
		while (powerOfTwo < original) {
			powerOfTwo <<= 1;
			i++;
		}
		System.out.println("使用while：" + i);
	}

	public static Instant toInstant(LocalDateTime dateTime) {
		return dateTime.atZone(ZoneId.systemDefault()).toInstant();
	}

	public static Date toDate(LocalDateTime dateTime) {
		return Date.from(toInstant(dateTime));
	}
}
